<?php
	include"DbClass.php";
	
	class DbBak{
	
		private $host="localhost";
		private $user="root";
		private $pass="";
		private $sql_creat;   //备份sql的内容	
		private $file_data;
		private $filenum;
		private $time;
		private $path;   //存入文件夹
		private $fileDri;
		private $dbName;
		private $charset="UTF8";
		
		
		function DbBak($dbName,$fileDri){
			
			$this->dbName=$dbName;
			$this->connect();
			$this->fileDri=$this->fileName($fileDri);
			$this->filenum=fopen($this->fileDri,"a+");
		}
		
		//-----------连接数据库------------------
		private function connect(){
		
			$link=mysql_connect($this->host,$this->user,$this->pass) or die ($this->error());
			
			mysql_select_db($this->dbName,$link) or die("没该数据库：".$this->table);
			  
			mysql_query("SET NAMES " .$this->charset)	or die ($this->error());  //转码
		}
		
		function error(){
		
			return $this->error=mysql_error();
		}
		
		//表的基本信息
		function  showData(){
			
			
			$sql="SHOW TABLE STATUS";
			
			$result=mysql_query($sql);
			
			while($row=mysql_fetch_assoc($result)){
				
				$this->sql_creat.= ' 表名：'.$row['Name']." ";
				
				$this->sql_Data_length=$row['Data_length'];//数据大小,单位为B
				
				$this->sql_Data_length=ceil($this->sql_Data_length/1024).' K';//换算成KB
				
				$this->sql_creat .="表数据量：".$this->sql_Data_length." ";
				
				$this->sql_creat .="建表时间:".$row['Create_time']." ";//表建立的时间
				
				$this->sql_creat .="表注释:".$row['Comment'];//表的注释
				
				$this->sql_creat=str_replace("-"," ",$this->sql_creat);
				
				$this->sql_creat=str_replace(":"," ",$this->sql_creat);
				
				$this->sql_creat="\n\n\n\n\n\n"."--\n".'-- '.$this->sql_creat."\n--\n"."\n\n";
				
				$this->writeToFile($this->sql_creat);
				
				$this->sql_creat="";
				
				$this->showTable($row['Name']);
				
				$this->tableData($row['Name']);
			}
			
			return $this->sql_creat;
		}
		
				
		//创建表的语言
		function showTable($tableName){
			
			$sql="SHOW CREATE TABLE $tableName";
			
			$result=mysql_query($sql)or die(mysql_error());
			
			while($row=mysql_fetch_array($result)){
				
				$row=$row[1].";\n\n\n";
				
				$this->writeToFile($row);
				
			//	var_dump( $row[1]);
				
			}
		}
		
			//获取数据
		function tableData($tableName){
			
			$fieldInfo=$this->_getFieldInfo($tableName);
			
			$key = array_keys($fieldInfo);
			
			$key = implode('`,`',$key);
		
			$sql2="INSERT INTO `$tableName` (`$key`) VALUES \n";    //插入表头
			
			$sql="SELECT * FROM $tableName";
			
			$result=mysql_query($sql);
			
			$number=mysql_num_rows($result);
			
			if($number>0){
			
				$this->writeToFile($sql2);
			}
			
			$t=0;
			
			while($row=mysql_fetch_assoc($result)){
				$t++;
			//	echo $t;
			//	var_dump($row);
				$row=$this->_quoteRow($fieldInfo,$row);
				if($t==$number){
					$record='(' . implode(',',$row) . ");\r\n";
					echo $t;
				}else{
				$record='(' . implode(',',$row) . "),\r\n";
				}
			//	$record=@Iconv('GB2312','UTF-8',$record);
				$this->writeToFile($record);
			}
		}
	
		//数据处理
		function _quoteRow($fieldInfo,$row){
			
			foreach ($row as $field_name=>$field_value){
			
				$field_value=strval($field_value);
				
				switch($fieldInfo[$field_name]){  
				
					case "blob":     $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;                          
					case "string":   $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;  
					case "date":          $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;  
					case "datetime": $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;  
					case "time":          $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;  
					case "unknown":  $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;                                         
					case "int":          $row[$field_name] = intval($field_value); break;
					case "real":     $row[$field_name] = intval($field_value); break;
					case "timestamp":$row[$field_name] = intval($field_value); break;
					default:                  $row[$field_name] = intval($field_value); break;
				}
			}
                return $row;
        }
		
		//获取字段信息
		 function _getFieldInfo($tableName){
                $fieldInfo = array();
                $sql="SELECT * FROM $tableName LIMIT 1";
                $result = mysql_query($sql);
                $num_field=mysql_num_fields($result);
                for($i=0;$i<$num_field;$i++){
                        $field_name=mysql_field_name($result,$i);
                        $field_type=mysql_field_type($result,$i);
                        $fieldInfo[$field_name] = $field_type;
                }
                mysql_free_result($result);
                return $fieldInfo;
        }
	
	
		//文件夹
		function fileName($path){
			
			$this->path=$path;
			
			$this->time=time();
			
		    $this->time=date('y-m-d' ,$this->time);
			
			$this->fileName=$this->path.'/'.$this->time.".sql";
			
			if(!file_exists($this->path)){
				//如果不存在生成这个目录,0777表示最大的读写权限
				mkdir($path,0777);
			}
			
			if(!file_exists($this->fileName)){
			
				@fopen($filename,"w");
			}
			
			return $this->fileName;
		}
		
		//数据写入
		function writeToFile($data){
			
			fwrite($this->filenum,$data);
			
		//	ftruncate($this->filenum,strlen($data));	
		}
		
		//运行函数
		function run(){
		//	flock($this->filenum,LOCK_EX);
			$this->showData();
			fclose($this->filenum);
		}

	}
//	
	$dbbak=new DbBak("lb_weibo","mysql");
	$dbbak->run();
	
?>